<template>
  <div class="event-detail-container">
    <!-- 赛事基本信息 -->
    <el-card class="event-info-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">赛事详情</span>
          <div class="actions">
            <el-button @click="$router.back()">
              <IconifyIconOffline icon="ep:back" class="mr-1" />
              返回
            </el-button>
            <el-button type="primary" @click="handleEdit">
              <IconifyIconOffline icon="ep:edit" class="mr-1" />
              编辑
            </el-button>
          </div>
        </div>
      </template>

      <div class="event-content">
        <div class="event-header">
          <div class="cover-section">
            <img :src="event.cover" :alt="event.name" class="event-cover" />
          </div>

          <div class="info-section">
            <h1 class="event-title">{{ event.name }}</h1>
            <p class="event-subtitle">{{ event.subtitle }}</p>

            <div class="event-meta">
              <div class="meta-item">
                <IconifyIconOffline icon="ep:trophy" class="meta-icon" />
                <span
                  >{{ getSportTypeName(event.sportType) }} ·
                  {{ getLevelName(event.level) }}</span
                >
              </div>

              <div class="meta-item">
                <IconifyIconOffline icon="ep:calendar" class="meta-icon" />
                <span
                  >{{ formatDateTime(event.startDate) }} -
                  {{ formatDateTime(event.endDate) }}</span
                >
              </div>

              <div class="meta-item">
                <IconifyIconOffline icon="ep:location" class="meta-icon" />
                <span
                  >{{ event.province }}{{ event.city }}{{ event.district }}
                  {{ event.address }}</span
                >
              </div>

              <div class="meta-item">
                <IconifyIconOffline
                  icon="ep:office-building"
                  class="meta-icon"
                />
                <span>{{ event.venue }}</span>
              </div>

              <div class="meta-item">
                <IconifyIconOffline icon="ep:user-filled" class="meta-icon" />
                <span
                  >{{ event.currentTeams }}/{{ event.maxTeams }} 队伍 ({{
                    event.teamSize
                  }}人/队)</span
                >
              </div>

              <div class="meta-item">
                <IconifyIconOffline icon="ep:money" class="meta-icon" />
                <span v-if="event.registrationFee === 0" class="free-tag"
                  >免费</span
                >
                <span v-else class="fee-tag"
                  >¥{{ event.registrationFee }}/队</span
                >
              </div>
            </div>

            <div class="status-section">
              <el-tag :type="getStatusType(event.status)" size="large">
                {{ getStatusText(event.status) }}
              </el-tag>

              <el-tag v-if="event.isPublic" type="success" size="large">
                公开赛事
              </el-tag>

              <el-tag v-if="event.needApproval" type="warning" size="large">
                需要审核
              </el-tag>
            </div>
          </div>
        </div>

        <div class="event-description">
          <h3>赛事介绍</h3>
          <p>{{ event.description }}</p>
        </div>

        <div v-if="event.rules" class="event-rules">
          <h3>赛事规则</h3>
          <p>{{ event.rules }}</p>
        </div>
      </div>
    </el-card>

    <!-- 时间安排 -->
    <el-card class="schedule-card" shadow="never">
      <template #header>
        <span class="title">时间安排</span>
      </template>

      <div class="schedule-content">
        <el-timeline>
          <el-timeline-item timestamp="报名阶段" placement="top" type="primary">
            <div class="timeline-content">
              <div class="timeline-title">报名时间</div>
              <div class="timeline-desc">
                {{ formatDateTime(event.registrationStartDate) }} -
                {{ formatDateTime(event.registrationEndDate) }}
              </div>
            </div>
          </el-timeline-item>

          <el-timeline-item timestamp="比赛阶段" placement="top" type="success">
            <div class="timeline-content">
              <div class="timeline-title">比赛时间</div>
              <div class="timeline-desc">
                {{ formatDateTime(event.startDate) }} -
                {{ formatDateTime(event.endDate) }}
              </div>
            </div>
          </el-timeline-item>
        </el-timeline>
      </div>
    </el-card>

    <!-- 主办方信息 -->
    <el-card class="organizer-card" shadow="never">
      <template #header>
        <span class="title">主办方信息</span>
      </template>

      <div class="organizer-content">
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="organizer-item">
              <div class="organizer-label">主办方</div>
              <div class="organizer-value">{{ event.organizer }}</div>
            </div>
          </el-col>

          <el-col v-if="event.coOrganizer" :span="8">
            <div class="organizer-item">
              <div class="organizer-label">协办方</div>
              <div class="organizer-value">{{ event.coOrganizer }}</div>
            </div>
          </el-col>

          <el-col :span="8">
            <div class="organizer-item">
              <div class="organizer-label">联系人</div>
              <div class="organizer-value">{{ event.contactName }}</div>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" style="margin-top: 20px">
          <el-col :span="8">
            <div class="organizer-item">
              <IconifyIconOffline icon="ep:phone" class="contact-icon" />
              <div>
                <div class="organizer-label">联系电话</div>
                <div class="organizer-value">{{ event.contactPhone }}</div>
              </div>
            </div>
          </el-col>

          <el-col v-if="event.contactEmail" :span="8">
            <div class="organizer-item">
              <IconifyIconOffline icon="ep:message" class="contact-icon" />
              <div>
                <div class="organizer-label">联系邮箱</div>
                <div class="organizer-value">{{ event.contactEmail }}</div>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 奖项设置 -->
    <el-card class="prizes-card" shadow="never">
      <template #header>
        <span class="title">奖项设置</span>
      </template>

      <div class="prizes-content">
        <div class="prize-list">
          <div
            v-for="(prize, index) in event.prizes"
            :key="index"
            class="prize-item"
          >
            <div class="prize-rank">{{ index + 1 }}</div>
            <div class="prize-info">
              <div class="prize-name">{{ prize.name }}</div>
              <div class="prize-desc">{{ prize.description }}</div>
            </div>
          </div>
        </div>
      </div>
    </el-card>

    <!-- 参赛队伍 -->
    <el-card class="teams-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span class="title">参赛队伍 ({{ teams.length }})</span>
          <el-button type="primary" size="small" @click="exportTeams">
            <IconifyIconOffline icon="ep:download" class="mr-1" />
            导出名单
          </el-button>
        </div>
      </template>

      <el-table :data="teams" stripe>
        <el-table-column prop="id" label="序号" width="80" />
        <el-table-column prop="name" label="队伍名称" min-width="150" />
        <el-table-column prop="captain" label="队长" width="120" />
        <el-table-column prop="phone" label="联系电话" width="130" />
        <el-table-column prop="memberCount" label="队员数量" width="100">
          <template #default="{ row }">
            {{ row.memberCount }}/{{ event.teamSize }}
          </template>
        </el-table-column>
        <el-table-column prop="registrationTime" label="报名时间" width="160">
          <template #default="{ row }">
            {{ formatDateTime(row.registrationTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getTeamStatusType(row.status)">
              {{ getTeamStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          v-if="event.registrationFee > 0"
          prop="paymentStatus"
          label="支付状态"
          width="100"
        >
          <template #default="{ row }">
            <el-tag :type="getPaymentStatusType(row.paymentStatus)">
              {{ getPaymentStatusText(row.paymentStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button type="primary" link size="small" @click="viewTeam(row)">
              查看
            </el-button>
            <el-button type="danger" link size="small" @click="removeTeam(row)">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 操作记录 -->
    <el-card class="logs-card" shadow="never">
      <template #header>
        <span class="title">操作记录</span>
      </template>

      <el-timeline>
        <el-timeline-item
          v-for="log in operationLogs"
          :key="log.id"
          :timestamp="formatDateTime(log.createTime)"
          placement="top"
        >
          <div class="log-content">
            <div class="log-action">{{ log.action }}</div>
            <div class="log-operator">操作人：{{ log.operator }}</div>
            <div v-if="log.remark" class="log-remark">{{ log.remark }}</div>
          </div>
        </el-timeline-item>
      </el-timeline>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { IconifyIconOffline } from "@/components/ReIcon";
import dayjs from "dayjs";

const route = useRoute();
const router = useRouter();

// 赛事详情数据
const event = ref({
  id: 1,
  name: "2024年春季羽毛球锦标赛",
  subtitle: "全市业余羽毛球爱好者的盛会",
  sportType: "badminton",
  level: "city",
  description:
    "本次锦标赛旨在推广羽毛球运动，提高市民身体素质，促进羽毛球运动的普及和发展。比赛采用淘汰制，分为男单、女单、男双、女双、混双五个项目。欢迎全市羽毛球爱好者踊跃报名参加。",
  cover:
    "https://trae-api-sg.mchost.guru/api/ide/v1/text_to_image?prompt=badminton%20tournament%20poster%2C%20professional%20sports%20event%2C%20championship%20design&image_size=portrait_4_3",
  registrationStartDate: "2024-02-01 09:00:00",
  registrationEndDate: "2024-03-01 18:00:00",
  startDate: "2024-03-15 08:00:00",
  endDate: "2024-03-17 18:00:00",
  province: "广东省",
  city: "深圳市",
  district: "南山区",
  address: "科技园南区深南大道9988号",
  venue: "深圳湾体育中心羽毛球馆",
  maxTeams: 32,
  currentTeams: 24,
  teamSize: 2,
  registrationFee: 200,
  eligibility: "amateur",
  ageLimit: "18-35",
  organizer: "深圳市体育局",
  coOrganizer: "深圳市羽毛球协会",
  contactName: "张主任",
  contactPhone: "13800138000",
  contactEmail: "contact@sports.sz.gov.cn",
  prizes: [
    { name: "冠军", description: "奖杯 + 奖金5000元" },
    { name: "亚军", description: "奖杯 + 奖金3000元" },
    { name: "季军", description: "奖杯 + 奖金2000元" },
    { name: "优秀奖", description: "证书 + 奖金500元" }
  ],
  allowSubstitute: true,
  allowSpectator: true,
  isPublic: true,
  needApproval: false,
  rules:
    "1. 比赛采用21分制，三局两胜；2. 发球必须从下手位发出；3. 比赛中不得有不文明行为；4. 服装要求运动装，不得穿牛仔裤等非运动服装；5. 比赛时间不得迟到，迟到10分钟视为弃权。",
  status: "registration",
  creator: "管理员",
  createTime: "2024-01-15 10:00:00"
});

// 参赛队伍列表
const teams = ref([
  {
    id: 1,
    name: "飞羽队",
    captain: "李明",
    phone: "13800138001",
    memberCount: 2,
    registrationTime: "2024-02-05 10:30:00",
    status: "confirmed",
    paymentStatus: "paid"
  },
  {
    id: 2,
    name: "雄鹰队",
    captain: "王强",
    phone: "13800138002",
    memberCount: 2,
    registrationTime: "2024-02-06 14:20:00",
    status: "confirmed",
    paymentStatus: "paid"
  },
  {
    id: 3,
    name: "闪电队",
    captain: "张伟",
    phone: "13800138003",
    memberCount: 1,
    registrationTime: "2024-02-07 09:15:00",
    status: "pending",
    paymentStatus: "pending"
  }
]);

// 操作记录
const operationLogs = ref([
  {
    id: 1,
    action: "创建赛事",
    operator: "管理员",
    createTime: "2024-01-15 10:00:00",
    remark: "初始创建"
  },
  {
    id: 2,
    action: "开放报名",
    operator: "管理员",
    createTime: "2024-02-01 09:00:00",
    remark: "报名通道开启"
  },
  {
    id: 3,
    action: "队伍报名",
    operator: "飞羽队",
    createTime: "2024-02-05 10:30:00",
    remark: "在线支付成功"
  }
]);

// 格式化日期时间
const formatDateTime = (dateTime: string) => {
  return dayjs(dateTime).format("YYYY-MM-DD HH:mm");
};

// 获取运动类型名称
const getSportTypeName = (type: string) => {
  const typeMap: Record<string, string> = {
    basketball: "篮球",
    badminton: "羽毛球",
    football: "足球",
    tennis: "网球",
    pingpong: "乒乓球"
  };
  return typeMap[type] || type;
};

// 获取级别名称
const getLevelName = (level: string) => {
  const levelMap: Record<string, string> = {
    international: "国际级",
    national: "国家级",
    provincial: "省级",
    city: "市级",
    district: "区级",
    amateur: "业余级"
  };
  return levelMap[level] || level;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    preparing: "info",
    registration: "warning",
    ongoing: "primary",
    completed: "success",
    cancelled: "danger"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    preparing: "筹备中",
    registration: "报名中",
    ongoing: "进行中",
    completed: "已结束",
    cancelled: "已取消"
  };
  return statusMap[status] || "未知";
};

// 获取队伍状态类型
const getTeamStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    confirmed: "success",
    pending: "warning",
    rejected: "danger"
  };
  return statusMap[status] || "info";
};

// 获取队伍状态文本
const getTeamStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    confirmed: "已确认",
    pending: "待审核",
    rejected: "已拒绝"
  };
  return statusMap[status] || "未知";
};

// 获取支付状态类型
const getPaymentStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    paid: "success",
    pending: "warning",
    failed: "danger",
    refunded: "info"
  };
  return statusMap[status] || "info";
};

// 获取支付状态文本
const getPaymentStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    paid: "已支付",
    pending: "待支付",
    failed: "支付失败",
    refunded: "已退款"
  };
  return statusMap[status] || "未知";
};

// 编辑赛事
const handleEdit = () => {
  router.push(`/event/edit/${event.value.id}`);
};

// 查看队伍
const viewTeam = (team: any) => {
  ElMessage.info(`查看队伍：${team.name}`);
};

// 移除队伍
const removeTeam = async (team: any) => {
  try {
    await ElMessageBox.confirm(
      `确定要移除队伍 "${team.name}" 吗？`,
      "确认移除",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }
    );

    // 这里应该调用API移除队伍
    ElMessage.success("移除成功");
  } catch {
    // 用户取消
  }
};

// 导出队伍名单
const exportTeams = () => {
  // 这里应该调用导出API
  ElMessage.success("导出成功");
};

// 加载赛事详情
const loadEventDetail = async () => {
  const eventId = route.params.id;
  // 这里应该根据ID调用API获取赛事详情
  console.log("加载赛事详情:", eventId);
};

onMounted(() => {
  loadEventDetail();
});
</script>

<style scoped lang="scss">
.event-detail-container {
  padding: 20px;
  background-color: #f5f7fa;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 18px;
    font-weight: 600;
    color: #303133;
  }

  .actions {
    display: flex;
    gap: 8px;
  }
}

.event-info-card,
.schedule-card,
.organizer-card,
.prizes-card,
.teams-card,
.logs-card {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.event-content {
  .event-header {
    display: flex;
    gap: 30px;
    margin-bottom: 30px;

    .cover-section {
      flex-shrink: 0;

      .event-cover {
        width: 200px;
        height: 267px;
        object-fit: cover;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      }
    }

    .info-section {
      flex: 1;

      .event-title {
        font-size: 24px;
        font-weight: 600;
        color: #303133;
        margin: 0 0 8px 0;
      }

      .event-subtitle {
        font-size: 16px;
        color: #606266;
        margin: 0 0 20px 0;
      }

      .event-meta {
        margin-bottom: 20px;

        .meta-item {
          display: flex;
          align-items: center;
          margin-bottom: 12px;

          .meta-icon {
            font-size: 16px;
            color: #909399;
            margin-right: 8px;
            width: 16px;
          }

          .free-tag {
            color: #67c23a;
            font-weight: 600;
          }

          .fee-tag {
            color: #e6a23c;
            font-weight: 600;
          }
        }
      }

      .status-section {
        display: flex;
        gap: 8px;
        flex-wrap: wrap;
      }
    }
  }

  .event-description,
  .event-rules {
    margin-bottom: 20px;

    h3 {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
      margin: 0 0 12px 0;
    }

    p {
      color: #606266;
      line-height: 1.6;
      margin: 0;
    }
  }
}

.schedule-content {
  .timeline-content {
    .timeline-title {
      font-weight: 600;
      color: #303133;
      margin-bottom: 4px;
    }

    .timeline-desc {
      color: #606266;
      font-size: 14px;
    }
  }
}

.organizer-content {
  .organizer-item {
    display: flex;
    align-items: center;
    gap: 12px;

    .contact-icon {
      font-size: 20px;
      color: #409eff;
    }

    .organizer-label {
      font-size: 14px;
      color: #909399;
      margin-bottom: 4px;
    }

    .organizer-value {
      font-size: 16px;
      color: #303133;
      font-weight: 500;
    }
  }
}

.prizes-content {
  .prize-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;

    .prize-item {
      display: flex;
      align-items: center;
      padding: 16px;
      background-color: #f8f9fa;
      border-radius: 8px;
      border-left: 4px solid #409eff;

      .prize-rank {
        width: 40px;
        height: 40px;
        background-color: #409eff;
        color: white;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        margin-right: 12px;
      }

      .prize-info {
        flex: 1;

        .prize-name {
          font-weight: 600;
          color: #303133;
          margin-bottom: 4px;
        }

        .prize-desc {
          font-size: 14px;
          color: #606266;
        }
      }
    }
  }
}

.log-content {
  .log-action {
    font-weight: 600;
    color: #303133;
    margin-bottom: 4px;
  }

  .log-operator {
    font-size: 12px;
    color: #909399;
    margin-bottom: 2px;
  }

  .log-remark {
    font-size: 12px;
    color: #606266;
  }
}

@media (max-width: 768px) {
  .event-detail-container {
    padding: 10px;
  }

  .event-header {
    flex-direction: column;
    gap: 20px;

    .cover-section .event-cover {
      width: 100%;
      max-width: 200px;
      height: auto;
    }
  }

  .card-header {
    flex-direction: column;
    gap: 10px;
    align-items: flex-start;

    .actions {
      width: 100%;
      justify-content: flex-end;
    }
  }

  .prize-list {
    grid-template-columns: 1fr;
  }
}
</style>
